import Utils from "../utils";

// 操作数据解析工具类
let OperationParser = {
   /**
    * 解析 Operation 数组
    * @param {Array} data  操作数据数组
    * @param {object} faNoMap 设备编号与 y轴下标映射关系
    * @param {Array} fas 设备数组
    */
   parse: data => {
      let series = [],
         heatsId = [];
      data.forEach(d => {
         series.push(OperationParser.parseSingle(d));
         heatsId.push(d.operationRoute.heatSid);
      });

      return {
         series: series,
         heatsId: heatsId
      }
   },

   // 解析单个 Operation 数据（包含 operationRoute 和 operations）
   parseSingle: data => {
      let serie = OperationParser.parseRoute(data.operationRoute),
         d = null,
         operationCcmFacilityNo = null,
         colorByCcm = null;
      serie.data = [];
      
      data.operations.forEach(o => {
          if(o.facilityNo.substring(0,3)==='CCM'){
            operationCcmFacilityNo = o.facilityNo;
            colorByCcm = OperationParser.getOperationRouteColor(operationCcmFacilityNo);
            serie.color = colorByCcm;
          }
          d = OperationParser.parseOperation(o);
         if (d) {
            serie.data.push(d);
         }
      });
      // 记录每个数据列的开始和结束时间
      if (serie.data.length > 0) {
         serie.extra.min = serie.data[0].x;
         serie.extra.max = serie.data[serie.data.length - 1].x2;
      }
      return serie;
   },

   // 解析 operationRoute
   parseRoute: route => {
      return {
         id: route.sid,
         name: route.routeId,
         heatSid: route.heatSid,
         extra: {
            planEndTime: route.planEndTime,
            planStartTime: route.planStartTime,
            version: route.version
         }
      }
   },
   //根据铸机指定作业路径的颜色
   getOperationRouteColor:operationCcmFacilityNo =>{
       let colorByCcm = null,
       colors = [
         ['#6C2391', '#8446A3', '#9B68B1', '#B38ACA', '#C6ACD0'],
         ['#00b6c7', '#00d2e6', '#0aeaff', '#42efff', '#7af4ff'],
         ['#23906B', '#45A381', '#69B39A', '#89C4B1', '#ACD5C8']
       ];
       colors.forEach((c, index) => {
        let color = {
              linearGradient: {
                 x1: 0,
                 y1: 0,
                 x2: 0,
                 y2: 1
              },
              stops: []
           },
           i = 0,
           total = 11,
           cIndex = 0;
        for (; i < total; i++) {
           cIndex = i - 1;
           if (cIndex >= 5) {
              cIndex = 8 - cIndex;
           }
           if (cIndex < 0) {
              cIndex = 0;
           }
           color.stops.push([parseFloat((0.1 * i).toFixed(2)), c[cIndex]]);
        }
        colors[index] = color;
     });
       if(operationCcmFacilityNo === 'CCM5'){
        colorByCcm=colors[0];
       }else if(operationCcmFacilityNo === 'CCM6'){
        colorByCcm=colors[1];
       }else if(operationCcmFacilityNo === 'CCM7'){
        colorByCcm=colors[2];
       }
     return colorByCcm
   },
   parseOperation: operation => {
      // 解析 operation

      let x = null,
         x2 = null,
         y = Utils.facilitiesNoMap[operation.facilityNo];
      if (y === undefined) {
         return null;
      }

      if (operation.actualStartTime) {
         x = new Date(operation.actualStartTime).getTime();
         if (operation.actualEndTime) {
            x2 = new Date(operation.actualEndTime).getTime();
         } else {
            x2 = new Date(operation.planEndTime).getTime() - new Date(operation.planStartTime).getTime() + x;
         }
      } else {
         x = new Date(operation.planStartTime).getTime();
         x2 = new Date(operation.planEndTime).getTime();
      }

      return {
         y: y + (Utils.facilities[y].facilityType === 'D' && operation.stationNo === 1 ? 0.25 : 0.75), // 双工位
         x: x,
         x2: x2,
         extra: {
            facilityNo: operation.facilityNo,
            operationSeq: operation.operationSeq,
            operationStat: operation.operationStat,
            sid: operation.sid,
            routeSid: operation.routeSid,
            heatSid: operation.heatSid,
            actualStartTime: operation.actualStartTime,
            actualEndTime: operation.actualEndTime,
            planStartTime: operation.planStartTime,
            planEndTime: operation.planEndTime,
            version: operation.version,
            actualFacilityNo: operation.actualFacilityNo,
            stationNo: operation.stationNo || 1
         }
      }
   },

   // 转换成 route 数据
   toRoute: serie => {
      return {
         sid: serie.options.id,
         routeId: serie.options.name,
         heatSid: serie.options.heatSid,
         planStartTime: serie.options.extra.planStartTime,
         planEndTime: serie.options.extra.planEndTime,
         version: serie.options.extra.version
      }
   },

   // 转换成 operation 数据
   toOperation: point => {
      return {
         sid: point.extra.sid,
         routeSid: point.extra.routeSid,
         planStartTime: Utils.dateFormat(point.x, '-', true),
         planEndTime: Utils.dateFormat(point.x2, '-', true),
         operationStat: point.extra.operationStat,
         operationSeq: point.extra.operationSeq,
         heatSid: point.extra.heatSid,
         facilityNo: Utils.facilities[Math.floor(point.y)].facilityNo,
         actualEndTime: point.extra.actualEndTime,
         actualFacilityNo: point.extra.actualFacilityNo,
         actualStartTime: point.extra.actualEndTime,
         version: point.extra.version,
         stationNo: point.extra.stationNo || 1
      }
   }
};

export default OperationParser;